//
//  UIButton+Indicator.m
//  UIButton Indicator
//
//  Copyright (c) 2015 Jeremiah Poisson
//

#import "UIButton+Indicator.h"
#import <objc/runtime.h>

// Associative reference keys.
static NSString *const kIndicatorViewKey = @"indicatorView";
static NSString *const kHintLabelKey = @"hintLabel";
static NSString *const kButtonTextObjectKey = @"buttonTextObject";

@implementation UIButton (Indicator)

- (void) showIndicator {
    
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    indicator.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
    [indicator startAnimating];
    
    NSString *currentButtonText = self.titleLabel.text;
    
    objc_setAssociatedObject(self, &kButtonTextObjectKey, currentButtonText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, &kIndicatorViewKey, indicator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    [self setTitle:@"" forState:UIControlStateNormal];
    self.enabled = NO;
    [self addSubview:indicator];

}
- (void) showIndicatorWithText:(NSString *)indicatorText {
    
    UIColor *btnColor = self.titleLabel.textColor;
    UIFont *btnFont = self.titleLabel.font;
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGRect rect = [indicatorText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:options attributes:@{NSFontAttributeName:btnFont,NSForegroundColorAttributeName:btnColor} context:nil];
    
    CGFloat indicatorW = 35;
    CGFloat textW = rect.size.width;
    CGFloat buttonW = self.bounds.size.width;
    
    CGFloat indicatorX = (buttonW-indicatorW-textW)/2;
    CGFloat indicatorY = (self.bounds.size.height-indicatorW)/2;
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    indicator.frame = CGRectMake(indicatorX, indicatorY, indicatorW, indicatorW);
    [indicator startAnimating];
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = btnColor;
    label.font = btnFont;
    label.text = indicatorText;
    label.numberOfLines = 1;
    label.frame = CGRectMake(indicatorX+indicatorW, indicatorY, textW, indicatorW);
 
    NSString *currentButtonText = self.titleLabel.text;
    objc_setAssociatedObject(self, &kButtonTextObjectKey, currentButtonText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, &kIndicatorViewKey, indicator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, &kHintLabelKey, label, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    [self setTitle:@"" forState:UIControlStateNormal];
    self.enabled = NO;
    [self addSubview:indicator];
    [self addSubview:label];
}

- (void) hideIndicator {
    
    NSString *currentButtonText = (NSString *)objc_getAssociatedObject(self, &kButtonTextObjectKey);
    UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)objc_getAssociatedObject(self, &kIndicatorViewKey);
    
    [indicator removeFromSuperview];
    [self setTitle:currentButtonText forState:UIControlStateNormal];
    self.enabled = YES;
    
}
- (void)hideTextIndicator {
    NSString *currentButtonText = (NSString *)objc_getAssociatedObject(self, &kButtonTextObjectKey);
    UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)objc_getAssociatedObject(self, &kIndicatorViewKey);
    UILabel *label = (UILabel *)objc_getAssociatedObject(self, &kHintLabelKey);
    
    [indicator removeFromSuperview];
    if (label) [label removeFromSuperview];
    [self setTitle:currentButtonText forState:UIControlStateNormal];
    self.enabled = YES;
}
@end
